﻿using Mirror.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mirror.Strategies
{
    class MirrorStrategy : Strategy
    {
        protected double _multiplier;
        
        public MirrorStrategy(StrategySettings settings) : base(settings)
        {
            
        }

        public override String Name { get { return "Mirror";  } }

        protected override int ParametersCount
        {
            get { return 1; }
        }

        protected override bool ParseParameters()
        {
            return Double.TryParse(this._parameters[0], NumberStyles.Any, new CultureInfo("en-US"), out this._multiplier);
        }


        protected override bool CalculateTargetVolume(out double TargetVolume) {

            TargetVolume = 0;
            int k = 0;

            foreach (Symbol symbol in _symbols)
            {
                if ((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds - symbol.TimeStamp < Strategy.SymbolTimeout)
                {
                    TargetVolume += (symbol.Long - symbol.Short) / 2;
                    k++;
                }
            }

            if (k == 0)
            {
                return false;
            }

            TargetVolume /= k;

            TargetVolume *= this._multiplier;
            
            return true;
        }
        
    }
}
